package cn.edu.hust.sse.software7.class3.DAO;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.edu.hust.sse.software7.class3.common.PMF;
import cn.edu.hust.sse.software7.class3.entities.EBTrading;

public class EBTradingDAO {
	private static final Logger log = Logger.getLogger(EBTradingDAO.class
			.getName());

	public void addTrading(EBTrading trade) {

		log.info("add trading record on product " + trade.getProductId()
				+ " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(trade);
			tx.commit();
			log.finest("add trading record on product " + trade.getProductId()
					+ " success.");
		} catch (Exception e) {
			log.info("add trading record on product " + trade.getProductId()
					+ " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public List<EBTrading> getTradingHistoryByUser(Long userId) {
		log.info("get Trading History By User " + userId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBTrading> trades = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBTrading.class);
			query.setFilter("userId == '" + userId + "'");
			trades = (List<EBTrading>) query.execute();
			tx.commit();
			log.finest("get Trading History By User " + userId + " success.");
		} catch (Exception e) {
			log.info("get Trading History By User " + userId + " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return trades;
	}

	public void deleteTradingHistoryByProd(Long prodId) {
		log.info("delete Trading History By Prod " + prodId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();		
		try {
			tx.begin();
			Query query = pm.newQuery(EBTrading.class);
			query.setFilter("productId == '" + prodId + "'");
			List<EBTrading> trades = (List<EBTrading>) query.execute();
			pm.deletePersistentAll(trades);
			tx.commit();
			log.finest("delete Trading History By Prod " + prodId + " success.");
		} catch (Exception e) {
			log.info("delete Trading History By Prod " + prodId + " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public void deleteTradingHistoryByUser(Long userId) {
		log.info("delete Trading History By User " + userId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();		
		try {
			tx.begin();
			Query query = pm.newQuery(EBTrading.class);
			query.setFilter("userId == '" + userId + "'");
			List<EBTrading> trades = (List<EBTrading>) query.execute();
			pm.deletePersistentAll(trades);
			tx.commit();
			log.finest("delete Trading History By User " + userId + " success.");
		} catch (Exception e) {
			log.info("delete Trading History By User " + userId + " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public EBTrading getTradingById(Long tradeId) {
		log.info("get Trading By Id " + tradeId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		EBTrading trade = null;
		try {
			tx.begin();
			trade = pm.getObjectById(EBTrading.class, tradeId);
			tx.commit();
			log.finest("get Trading By Id " + tradeId + " success.");
		} catch (Exception e) {
			log.info("get Trading By Id " + tradeId + " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return trade;
	}

	public List<EBTrading> getAllTradingHistory() {
		log.info("get All Trading History begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBTrading> trades = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBTrading.class);
			trades = (List<EBTrading>) query.execute();
			tx.commit();
			log.finest("get All Trading History success.");
		} catch (Exception e) {
			log.info("get All Trading History failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return trades;
	}

	public List<EBTrading> getTradingHistoryByDateScope(Date from, Date to) {
		log.info("get Trading History By Date Scope from " + from + " to " + to + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBTrading> trades = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBTrading.class);
			query.setFilter("payTime >= '" + from + "'");
			query.setFilter("payTime <= '" + to + "'");
			trades = (List<EBTrading>) query.execute();
			tx.commit();
			log.finest("get Trading History By Date Scope from " + from + " to " + to + " success.");
		} catch (Exception e) {
			log.info("get Trading History By Date Scope from " + from + " to " + to + " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return trades;
	}

	public List<EBTrading> getTradingHistoryByProd(Long prodId) {
		log.info("get Trading History By Prod " + prodId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBTrading> trades = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBTrading.class);
			query.setFilter("productId == '" + prodId + "'");
			trades = (List<EBTrading>) query.execute();
			tx.commit();
			log.finest("get Trading History By Prod " + prodId + " success.");
		} catch (Exception e) {
			log.info("get Trading History By Prod " + prodId + " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return trades;
	}

}
